package com.example.zxingscan;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.google.zxing.client.android.CaptureActivity;
import com.google.zxing.client.android.came2.ScanActivity;

import static android.content.pm.PackageManager.PERMISSION_GRANTED;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.zxing).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.CAMERA)
                        ==PERMISSION_GRANTED) {
                    Intent i = new Intent(MainActivity.this, CaptureActivity.class);
                    startActivity(i);
                } else {
                    Toast.makeText(MainActivity.this,"缺少权限",Toast.LENGTH_LONG).show();
                }
            }
        });

        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.CAMERA)
                        ==PERMISSION_GRANTED) {
                    Toast.makeText(MainActivity.this,"已有权限",Toast.LENGTH_LONG).show();
                } else {
                    ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.CAMERA},1);
                }
            }
        });

        findViewById(R.id.zxing2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.CAMERA)
                        ==PERMISSION_GRANTED) {
                    Intent i = new Intent(MainActivity.this, ScanActivity.class);
                    startActivity(i);
                } else {
                    Toast.makeText(MainActivity.this,"缺少权限",Toast.LENGTH_LONG).show();
                }
            }
        });

    }
}
